package com.assist
{
	import flash.display.DisplayObject;
	import flash.display.Stage;

	public class URI
	{
		public static var test :Boolean = false;
		
		private static var _domain :String = "localhost";
		private static var _serverPort :uint = 8888;
		private static var _clientUrl :String = "http://sxd.xdcdn.net/2012051201/";
		private static var _assetsUrl :String;
		private static var _fullClientUrl :String;
		private static var _policyPort :uint = 888;
		private static var _args :Object = {};
		private static var _stage :Stage;
		private static var _version:Object = {};
		
		public function URI()
		{
		}
		
		public static function analyze( main :Main ) :void {
			_stage = main.stage;
			
			var index :Object = _stage.getChildAt( 1 );
			if( index.hasOwnProperty( "args" ) ) {
				_args = index.args;
			}
			
			if (_args["client_url"])
			{
				_clientUrl = _args["client_url"];
			}
			_assetsUrl = _clientUrl + "assets/";
			if (_clientUrl == "./")
			{
				_fullClientUrl = main.loaderInfo.url.replace(/(Main|rw)\.swf(\?[\s\S]+?)?/, "");
			}
			else
			{
				_fullClientUrl = _clientUrl;
			}
		}
		
		public static function get ip() : String
		{
			return _args["ip"] || _domain || "";
		}// end function
		
		public static function get port() : int
		{
			return _args["port"] || _serverPort || 0;
		}
		
		public static function get clientUrl() : String
		{
			return _clientUrl;
		}
		
		public static function get assetsRnd() : String
		{
			return _args["assets_rnd"] || "";
		}
		
		public static function formatVersion(versionData :String) : void {
			var versionArr :Array = versionData.split(/\r\n|\r|\n/g);
			for each( var versionStr :String in versionArr ) {
				var arr :Array = versionStr.split("|");
				if( arr.length == 2 ) {
					_version[ arr[0] ] = arr[ 1 ];
				}
			}
		}
		
		public static function getVersion(path :String) : String {
			path = path.replace( _assetsUrl, "" );
			var arr :Array = path.split("/");
			var len :uint = arr.length;
			if( len > 1 ) {
				path = "";
				for(var i :uint = 0; i < len - 1; ++i) {
					path += "/" + arr[ i ].substr(0, 1);
				}
				path += arr[ len - 1 ];
			}
			else {
				path = "/" + path;
			}
			
			return _version[ path ] || "";
		}
		
		public static function get isBaidu() : Boolean
		{
			return parseInt(_args["baidu"]) == 1;
		}
		
		public static function get isMangguo() : Boolean
		{
			return parseInt(_args["mangguo"]) == 1;
		}
		
		public static function get isPlatformB() : Boolean
		{
			return parseInt(_args["platform_b"]) == 1;
		}
	}
}